class Tools::ZhuiTianYaHotPostsController < ApplicationController
  before_filter :authorize, :except => [:new_and_hot, :auto_complete, :preview]
  layout 'base.html', :only => [:show, :new, :index,:preview]
  
  def my_settings
  end
  
  def new_and_hot
    index
  end
  
  def my_zhui    
    params[:hot_post_type_id] = '%' if params[:hot_post_type_id].blank?
    @types = HotPostType::TianYa.all_with_power    
    @hot_posts = HotPost.all( 
      :conditions => [
        'hot_posts.hot_post_type_id like ? and follow_hot_post_settings.user_id = ? ',
        params[:hot_post_type_id],
        current_user_id
      ],
      :include => :follow_hot_post_settings
    )
  end
  
  def new    
    @hot_post = HotPost::TianYa.new(params[:hot_post_tian_ya])
    @hot_post.user = current_user    
    respond_to do |format|
      format.html do
        @seo_title = "麦吐司网-新增追天涯热帖"
        @seo_keywords = @seo_title
        @seo_description = @seo_title
      end
      format.js do
        @existed_one = @hot_post.existed_one_by_title
        render :layout => false
      end
    end
  end
  
  def create
    @hot_post = HotPost::TianYa.new(params[:hot_post_tian_ya])
    @hot_post.user = current_user
    if @hot_post.requested_url_error? 
      render :new
    elsif @hot_post.exists_by_title?
      logger.debug "已经被追"
      @hot_post = @hot_post.existed_one_by_title
      @hot_post.zhui! :user => current_user, :scope => params[:scope]
    elsif not @hot_post.save
      render :new
    else
      @hot_post.zhui! :user => current_user, :scope => params[:scope]
    end
  end
  
  def index
    params[:type] ||= 'new'
    params[:page] = 1 if params[:page].blank?
    params[:order_by] = params[:type] == 'hot' ?  'hot_posts.users_count desc' : 'hot_posts.created_at desc'
    params[:only_mine] ||= 'no'
    case
    when params[:hot_post_type_id].present?
      @hot_posts = params[:only_mine] == 'yes' ? (
        HotPost.paginate :conditions => [
            'hot_posts.hot_post_type_id like ? and follow_hot_post_settings.user_id = ? and follow_hot_post_settings.status = ?',
            params[:hot_post_type_id],
            current_user_id,
            'zhui'
          ],
          :include => :follow_hot_post_settings,
          :page => params[:page],
          :per_page => 30,
          :order => params[:order_by]
      ) : (
        HotPost.paginate :conditions => [
            'hot_posts.hot_post_type_id like ?',
            params[:hot_post_type_id]
          ],
          :page => params[:page],
          :per_page => 30,
          :order => params[:order_by]
      ) 
      
      def @hot_posts.hot_post_type_id=(hot_post_type_id)
        @hot_post_type_id = hot_post_type_id
      end
      
      def @hot_posts.current_user_zhuis_count
        FollowHotPostSetting.count(
          :conditions => [
            'follow_hot_post_settings.user_id = ? and follow_hot_post_settings.status = ?  and hot_posts.hot_post_type_id like ?',
            User.current_user.id,
            'zhui',
            @hot_post_type_id
          ],
          :include => :hot_post  
        ) 
      end
      @hot_posts.hot_post_type_id = params[:hot_post_type_id]
    when params[:key_words].present?
      @hot_posts = HotPost.paginate( 
          :conditions => [
            'title like ?',
            params[:key_words]
          ],
          :page => params[:page],
          :per_page => 30,
          :order => params[:order_by]
        )
      def @hot_posts.title_key_words=(key_words)
        @key_words = key_words
      end
      def @hot_posts.current_user_zhuis_count
        FollowHotPostSetting.count(
          :conditions => [
            'follow_hot_post_settings.user_id = ? and follow_hot_post_settings.status = ? and hot_posts.title like ?',
            User.current_user.id,
            'zhui',
            @key_words
          ],
          :include => :hot_post
        ) 
      end
      @hot_posts.title_key_words = params[:key_words]
    else
      @hot_posts = params[:only_mine] == 'yes' ? (
        HotPost.paginate :conditions => [
            'follow_hot_post_settings.user_id = ? and follow_hot_post_settings.status = ?',
            current_user_id,
            'zhui'
          ],
          :include => :follow_hot_post_settings,
          :page => params[:page],
          :per_page => 30,
          :order => params[:order_by]
      ) : (
        HotPost.paginate :page => params[:page],
          :per_page => 30,
          :order => params[:order_by]
      ) 
      def @hot_posts.current_user_zhuis_count
        FollowHotPostSetting.count(
          :conditions => [
            'user_id = ? and status = ?',
            User.current_user.id,
            'zhui'
          ]) 
      end      
    end
    respond_to do |format|
      format.html do
        @types = HotPostType::TianYa.all_with_power
        @seo_title = "麦吐司网-追天涯-短信、邮件提醒您所关注的天涯社区热帖的更新情况"
        @seo_keywords = @seo_title
        @seo_description = @seo_title
      end
      format.js do        
        render :layout => false
      end
    end
  end
  
  def show
    @hot_post = HotPost::TianYa.find(params[:id])
    @follow_setting = current_user.follow_hot_post_setting_by_hot_post_id(@hot_post.id)
    @follow_setting.scope!(params[:scope]) if params[:scope].present?
#    @follow_setting.send_sm
#    @follow_setting.send_email
    params[:page] ||= @follow_setting.read_page(:per_page => 30) 
    case
    when @follow_setting.scope.lz?
      @sub_hot_posts = SubHotPost.paginate( 
        :conditions => [
          'hot_post_id = ? and author = ?',
          @hot_post.id,
          @hot_post.author
        ],
        :page => params[:page],
        :per_page => 30,
        :order => 'id asc'
      )
    when @follow_setting.scope.all?
      @sub_hot_posts = SubHotPost.paginate( 
        :conditions => [
          'hot_post_id = ?',
          @hot_post.id
        ],
        :page => params[:page],
        :per_page => 30,
        :order => 'id asc'
      )
    else
      raise "未知范围：#{@follow_setting.scope}"
    end   
    def @sub_hot_posts.lz_sub_hot_posts_count
      each_with_object([]) do |sub_hot_post,sub_hot_posts|
        sub_hot_posts << sub_hot_post if sub_hot_post.author == sub_hot_post.hot_post.author
      end.size
    end
    respond_to do |format|
      format.html do
        @seo_title = "麦吐司网-追天涯-天涯热帖-#{@hot_post.hot_post_type.name}-#{@hot_post.title}"
        @seo_keywords = @seo_title
        @seo_description = @seo_title
      end
      format.js do        
        render :layout => false
      end
    end    
  end
  
  def preview
    @hot_post = HotPost::TianYa.find(params[:id])
    @sub_hot_posts = SubHotPost.paginate( 
      :conditions => [
        'hot_post_id = ?',
        @hot_post.id
      ],
      :page => params[:page],
      :per_page => 30,
      :order => 'id asc'
    )
    def @sub_hot_posts.lz_sub_hot_posts_count
      each_with_object([]) do |sub_hot_post,sub_hot_posts|
        sub_hot_posts << sub_hot_post if sub_hot_post.author == sub_hot_post.hot_post.author
      end.size
    end
    @seo_title = "麦吐司网-追天涯-天涯热帖-#{@hot_post.hot_post_type.name}-#{@hot_post.title}"
    @seo_keywords = @seo_title
    @seo_description = @seo_title
  end
  
  def show_zhui
    @follow_setting = current_user.follow_hot_post_setting_by_hot_post_id(params[:id])
  end
  
  def edit_zhui
    @hot_post = HotPost::TianYa.find(params[:id])
    @follow_setting = current_user.follow_hot_post_setting_by_hot_post_id(@hot_post.id)        
  end
  
  def update_zhui
    @hot_post = HotPost::TianYa.find(params[:id])
    @follow_setting = current_user.follow_hot_post_setting_by_hot_post_id(params[:id])
    @follow_setting.status = 'zhui'
    if @follow_setting.update_attributes params[:follow_hot_post_setting]
      render :show_zhui, :status => 200
    else
      render :edit_zhui, :status => 500
    end
  end
  
  def zhui
    @follow_setting = current_user.follow_hot_post_setting_by_hot_post_id(params[:id])
    @follow_setting.zhui!
    render :text => 'success', :status => 200
  end
  
  def give_up
    @follow_setting = current_user.follow_hot_post_setting_by_hot_post_id(params[:id])
    @follow_setting.give_up!
    render :text => 'success', :status => 200
  end
  
  # 纪录读
  def read
    @sub_hot_post = SubHotPost.find(params[:read_sub_hot_post_id])
    @follow_setting = current_user.follow_hot_post_setting_by_hot_post_id(params[:id])    
    @follow_setting.read!(@sub_hot_post)
    render :text => 'success', :status => 200
  end
  
  # 高亮or低亮楼主
  def high_or_low_light_lz
    @follow_setting = current_user.follow_hot_post_setting_by_hot_post_id params[:id]
    @follow_setting.lz_show_status.high_light? ? @follow_setting.ll_lz! : @follow_setting.hl_lz!
    render :text => 'success', :status => 200
  end
  
  def mines
    @setting = UserToolSetting.find(params[:setting_id])
    @follows = current_user.zhui_settings
    def @follows.top(how_many)
      each_with_object([]) do |setting, settings|
        settings << setting if settings.size <= how_many
      end
    end
    @news = HotPost::TianYa.all(
      :limit => 5,
      :order => 'created_at desc'
    )
    @hots = HotPost::TianYa.all(
      :limit => 5,
      :order => 'users_count desc'
    )
  end
  
  def destroy
    @setting = UserToolSetting::ZhuiTianYa.find(params[:id])
    render :text => '', :status => @setting.destroy ? 200 : 500
  end
  
  def auto_complete
    @hot_posts = HotPost.paginate(
      :conditions => [
        ' title like ?',
        "%#{params[:key_words]}%"
      ],
      :page => 1,
      :per_page => 20,
      :order => 'title asc',
      :limit => 20
    )
  end
end
